import { resolve } from "dns";
import config from "@/src/config/index.ts";
import {
    getToken
} from "@/src/utils/local";
// import {
//     tansParams
// } from "@/src/utils/tools.js";
export interface ResponseData<T> {
    code : number,
    data : T,
    message : string
}
class HttpRequest {
    baseUrl : string
    timeout : number
    httpDefaultOpts : any
    constructor(baseUrl : string, timeout : number) {
        this.baseUrl = baseUrl
        this.timeout = timeout
        this.httpDefaultOpts = {
            header: {
                "X-Requested-With": "XMLHttpRequest",
                Accept: "application/json",
                "Content-Type": "application/json; charset=UTF-8",
            },
            timeout: this.timeout,
        }
        if (getToken() && config.isToken) {
            this.httpDefaultOpts['token'] = getToken();
        }
    }
    get<T>(url : string, params ?: Record<string, string>) : Promise<ResponseData<T>> {
        if (params) {
            this.httpDefaultOpts['data'] = params;
        }
        return new Promise((resolve, reject) => {
            uni.request({
                url: this.baseUrl + url,
                method: "GET",
                ...this.httpDefaultOpts,
                success(res) {
                    resolve(res.data);
                },
                fail(err) {
                    reject(err);
                }
            })
        });
    }
    post() {

    }

    set header(header : object) {

    }
}
const request = new HttpRequest(config.baseUrl, config.timeout)
export default request;